package com.bigfish.gbs.handler;

import com.bigfish.gbs.dto.Result;
import com.bigfish.gbs.enums.ResultEnum;
import com.bigfish.gbs.exception.MyException;
import com.bigfish.gbs.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 全局异常处理
 * 使用@@ControllerAdvice和@ExceptionHandler实现全局Controller层的异常处理
 * Created by Joycessh
 * 11/25/18 7:34 PM
 */
@ControllerAdvice      //控制器增强，使用AOP对Controller控制器进行增强
@ResponseBody
public class GlobalExceptionHandler {

    /** 日志处理 */
    public final static Logger logger = LoggerFactory.getLogger(com.bigfish.gbs.handler.GlobalExceptionHandler.class);

    /**
     * 400错误 BAD REQUEST
     * @author Joycessh
     * @param e
     * @return com.bigfish.gbs.dto.Result
     * @date 11/25/18 9:38 PM
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {

        logger.error("[400 can not read json] {}", e);
        return ResultUtil.retFormat(ResultEnum.BAD_REQUEST);
    }

    /**
     * 400错误
     * @author Joycessh
     * @param e
     * @return com.bigfish.gbs.dto.Result
     * @date 11/25/18 11:56 PM
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handleValidationException(MethodArgumentNotValidException e) {

        logger.error("[method Argument not valid] {}", e);
        return ResultUtil.retFormat(ResultEnum.BAD_REQUEST);
    }

    /**
     * 400 找不到方法参数
     * @author Joycessh
     * @param e
     * @return com.bigfish.gbs.dto.Result
     * @date 11/26/18 12:21 AM
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Result handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {

        logger.error("[Miss Servlet Request Parameter] {}", e);
        return ResultUtil.retFormat(ResultEnum.BAD_REQUEST);
    }

    /**
     * 405错误
     * @author Joycessh
     * @param e
     * @return com.bigfish.gbs.dto.Result
     * @date 11/25/18 11:58 PM
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {

        logger.error("[request method not supported] {}", e);
        return ResultUtil.retFormat(ResultEnum.METHOD_NOT_ALLOWED);
    }

    /**
     * 500错误
     * @author Joycessh
     * @param e
     * @return com.bigfish.gbs.dto.Result
     * @date 11/25/18 11:58 PM
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({Exception.class})
    public Result handleException(Exception e) {

        logger.error("[Internal Server Error] {}", e);
        return ResultUtil.retFormat(ResultEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 处理所有自定义的业务异常
     * @author Joycessh
     * @param e
     * @return com.bigfish.gbs.dto.Result
     * @date 11/26/18 8:45 AM
     */
    @ExceptionHandler(MyException.class)
    public Result handleMyException(MyException e) {

        logger.error("[MyException] {}", e);
        return  ResultUtil.retFormat(e.getResultEnum());
    }
}
